package phase01.module4.code.question1;

import phase01.module4.code.question1.exceptions.InvalidIdException;

import java.util.Scanner;

/**
 * 1. 编程题
 *
 *   基于学生信息管理系统增加以下两个功能：
 *
 *   a.自定义学号异常类和年龄异常类，并在该成员变量不合理时产生异常对象并抛出。
 *
 *   b.当系统退出时将 List 集合中所有学生信息写入到文件中，当系统启动时读取文件中所 有学生信息到 List 集合中。
 */
@SuppressWarnings("unchecked")
public class StudentTest {
    private static final Database<Student> db = Database.getInstance();

    private static void onClose() {
        db.commit();
    }
    private static void onOpen() {
        db.load();
    }

    private static void insert(int id, String name, int age, String gender) throws Exception {
        Student s = new Student(id, name, age, gender);
        s = new Student(id, name, age, gender);
        db.insert(s);
    }

    private static void update(Student s) {
        db.update(s);
    }

    private static void delete(int id) throws InvalidIdException {
        Student s = new Student();
        s.setId(id);
        db.delete(s);
    }

    private static Student find(int id) throws InvalidIdException {
        Student s = new Student();
        s.setId(id);
        return db.find(s);
    }

    public static void main(String[] args) throws Exception {
        onOpen(); // 从文件载入学生信息
        int select = 0;
        Scanner sc = new Scanner(System.in);
        int id;
        String name;
        int age;
        String gender;
        Student s;
        do {
            System.out.println("请选择操作：");
            System.out.print("0.退出\t1.新建学生\n");
            System.out.print("2.更新学生\t3.删除学生\n");
            System.out.print("4.查找学生\t5.显示所有学生\n");
            System.out.println("=======================================================================================");
            select = sc.nextInt();
            switch (select) {
                case 1:
                    System.out.print("请输入学生信息(id 姓名 年龄 性别)：");
                    id = sc.nextInt();
                    name = sc.next();
                    age = sc.nextInt();
                    gender = sc.next();
                    insert(id, name, age, gender);
                    break;
                case 2:
                    System.out.print("请输入学生信息(id 姓名 年龄 性别)：");
                    id = sc.nextInt();
                    name = sc.next();
                    age = sc.nextInt();
                    gender = sc.next();
                    s = new Student(id, name, age, gender);
                    update(s);
                    break;
                case 3:
                    System.out.print("请输入学生ID：");
                    id = sc.nextInt();
                    delete(id);
                    break;
                case 4:
                    System.out.print("请输入学生ID：");
                    id = sc.nextInt();
                    s = find(id);
                    System.out.println("查找结果：" + s);
                    break;
                case 5:
                    db.show();
                    break;
                default:
                    break;
            }
        } while (select != 0);
        onClose(); // 学生信息写回文件
    }
}
